//
//  LSZResidentialAddressVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/29.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZResidentialAddressVC.h"

@interface LSZResidentialAddressVC ()<UITableViewDelegate,UITableViewDataSource,AddressViewDelegate,UITextViewDelegate>
@property(nonatomic,strong) UITableView * tableViewResidentialAddress;
@property (nonatomic, strong) YAddressPickerView *addView;

@end

@implementation LSZResidentialAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写居住地址";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableViewResidentialAddress registerClass:[LSZBasicInformationTableViewCell class] forCellReuseIdentifier:@"BasicInformation"];
    [self.tableViewResidentialAddress registerClass:[LSZStreetTableViewCell class] forCellReuseIdentifier:@"LSZStreetTableViewCell"];
    [self.tableViewResidentialAddress registerClass:[LSZSaveAddressFootCell class] forHeaderFooterViewReuseIdentifier:@"LSZSaveAddressFootCell"];
    _addView = [[YAddressPickerView alloc]init];
    [self.view addSubview:_addView];
    _addView.delegate = self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:50 :5];
        return height;
    }else{
        return 120;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 90;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    LSZSaveAddressFootCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZSaveAddressFootCell"];
    [cell.buttonSave addTarget:self action:@selector(clickSave) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LSZBasicInformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BasicInformation" forIndexPath:indexPath];
        cell.labelTitleType.text = @"所在地区";
        cell.labelContent.hidden = NO;
        cell.textFieldContent.hidden = YES;
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:18 :2];
        CGFloat A;
        if (ScreenWidth == 320) {
            A = 13;
        }else if(ScreenWidth == 375){
            A = 13.5;
        }else {
            A = 14;
        }
        CGFloat width = [LSZSpecialPackaging caculateWidthOfText:ScreenWidth height:height font:A :cell.labelTitleType.text]-10;
        CGRect rect = cell.labelTitleType.frame;
        rect.size.width = width;
        cell.labelTitleType.frame = rect;

        CGRect rectCont = cell.labelContent.frame;
        rectCont.origin.x = CGRectGetMaxX(cell.labelTitleType.frame);
        cell.labelContent.frame = rectCont;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头 
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    }else{
        LSZStreetTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZStreetTableViewCell" forIndexPath:indexPath];
        cell.textViewCont.delegate = self;
        cell.textViewCont.returnKeyType = UIReturnKeyDone;//改变为完成键，如果在项目中导入了YYText框架那么原生的就被替换掉了，变为returnKeyType = UIKeyboardTypeTwitter;
        cell.textViewCont.textColor = lightColor;
        cell.textViewCont.text = @"请输入详细地址信息,如道路、门牌号、小区、楼栋号、单元室等";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [textView resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    return YES;
}
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请输入详细地址信息,如道路、门牌号、小区、楼栋号、单元室等";
        textView.textColor = lightColor;
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请输入详细地址信息,如道路、门牌号、小区、楼栋号、单元室等"]){
        textView.text=@"";
        textView.textColor = titleColorLSZ;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self chooseAddress];
    }
}
-(void)chooseAddress
{
    [_addView show];
}
#pragma mark - 地区选择完毕
- (void)completingTheSelection:(NSString *)province city:(NSString *)city area:(NSString *)area{
    __weak typeof(self) weakSelf = self;
    NSString * str = [[NSString alloc]init];
    if ([province isEqualToString:city]) {
        str =  [NSString stringWithFormat:@"%@ %@",province,area];
    }else{
        str =  [NSString stringWithFormat:@"%@ %@ %@",province,city,area];
    }
    LSZBasicInformationTableViewCell * cell = (LSZBasicInformationTableViewCell *)[weakSelf.tableViewResidentialAddress cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0]];
    cell.labelContent.text = [NSString stringWithFormat:@"%@",str];
//    [weakSelf SetGenderOrArea:@"location" inputParameter:str];
}
#pragma mark - 确认
-(void)clickSave
{
    __weak typeof(self) weakSelf = self;
    LSZBasicInformationTableViewCell * cell = (LSZBasicInformationTableViewCell *)[weakSelf.tableViewResidentialAddress cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0]];
    LSZStreetTableViewCell * cellStreet = (LSZStreetTableViewCell *)[weakSelf.tableViewResidentialAddress cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1  inSection:0]];

    if (cell.labelContent.text.length == 0) {
        [self initWithtring:@"请选择所在地地址!"];
        return;
    }
    if (cellStreet.textViewCont.text.length == 0) {
        [self initWithtring:@"请填写详细街道信息!"];
        return;
    }
    UINavigationController *navigationVC = self.navigationController;
    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
    for (UIViewController *vc in navigationVC.viewControllers) {
        [viewControllers addObject:vc];
        if ([vc isKindOfClass:[LSZQualificationCertificationVC class]]) {
            LSZQualificationCertificationVC *messageVC =(LSZQualificationCertificationVC *)vc;
            messageVC.detailedAddressString = [NSString stringWithFormat:@"%@ %@",cell.labelContent.text,cellStreet.textViewCont.text];
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
}
-(UITableView *)tableViewResidentialAddress
{
    if (_tableViewResidentialAddress == nil) {
        _tableViewResidentialAddress = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableViewResidentialAddress.delegate = self;
        _tableViewResidentialAddress.dataSource = self;
        _tableViewResidentialAddress.separatorColor = divider;
        _tableViewResidentialAddress.estimatedRowHeight =0;
        _tableViewResidentialAddress.estimatedSectionFooterHeight =0;
        _tableViewResidentialAddress.estimatedSectionHeaderHeight =0;
        _tableViewResidentialAddress.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        [self.view addSubview:_tableViewResidentialAddress];
    }
    return _tableViewResidentialAddress;
}
@end
